﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class StoreManagerController : BaseController<store>
    {
        //
        // GET: /StoreManager/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }
        public override BLL.BaseBll<store> bll
        {
            get
            {
                return new StoreBll();
            }

        }

        ManagerBll managerBll = new ManagerBll();

        ManagerStoreBll managerStroreBll = new ManagerStoreBll();

        TempletBll tempBll = new TempletBll();

        WaresDetailsBll waresDetailBll = new WaresDetailsBll();


        public class StoreModel
        {
            public int storeid { get; set; }
            public string storeName { get; set; }
            public string storeDress { get; set; }
            public string RealName { get; set; }
            public string userPhone { get; set; }
            public string property { get; set; }
            public string discount { get; set; }
            public string state { get; set; }
            public int userid { get; set; }


        }
        //getone 的时候  用的 model
        public class StorGeteModel
        {
            public int userid { get; set; }
            public int storeid { get; set; }
            public string storeName { get; set; }
            public string storeDress { get; set; }
            public string RealName { get; set; }
            public string userPhone { get; set; }
            public int? property { get; set; }
            public decimal? discount { get; set; }
        }


        //加载数据列表 查询方法
        public override ActionResult Get(int page, int rows, store model)
        {
            int count;
            var list = bll.Search(model, x => true, true, x => x.storeid, rows, page, out count).ToList().Select(x => new
            {
                x.storeid,
                x.storeName,
                x.storeDress,
               property = x.property == 1 ? "大号" : x.property == 3 ? "小号" : x.property == 2 ? "结算台+冰箱" : x.property == 4 ? "结算台+货架" : "货架+冰箱",
                x.discount,
            }).ToList();
            return Json(new { total = count, rows = list });
        }

        //获取店铺列表  
        public ActionResult GetAll(int page, int rows, string storeName)
        {
            int count;
            var storeList = new List<StoreModel>();//Where(x => x.storeStatus == 1)  1.大号  2.中号 3.小号  货架型号
            if (storeName != "")
            {
                var list = bll.Search(x => x.storeStatus == 1 && x.storeName.Contains(storeName), true, x => x.storeid, rows, page, out count).
                    Select(x => new { x.storeid, x.storeName, x.storeDress, x.discount, property = x.property == 1 ? "大号" : x.property == 3 ? "小号" : x.property == 2 ? "结算台+冰箱" : x.property == 4 ? "结算台+货架" : "货架+冰箱" }).ToList();
                foreach (var item in list)
                {
                    string storeid = item.storeid.ToString();
                    //查询该店铺 对应的理货员 id   
                    int managerCode = managerStroreBll.Search(x => x.storeid == storeid).Select(x => new { x.managerCode }).FirstOrDefault().managerCode;
                    //根据 managerid  查询 manager表的信息
                    string name = managerBll.Search(x => x.userid == managerCode && x.state == 1).Select(x => new { x.RealName, }).FirstOrDefault().RealName;
                    //添加到 list 集合
                    StoreModel storemodel = new StoreModel();
                    storemodel.storeid = item.storeid;
                    storemodel.storeName = item.storeName;
                    storemodel.storeDress = item.storeDress;
                    storemodel.RealName = name;
                    storemodel.property = item.property;
                    storemodel.discount = item.discount.ToString();
                    storeList.Add(storemodel);
                }
            }
            else
            {
                var list = bll.Search(x => x.storeStatus == 1, true, x => x.storeid, rows, page, out count).Select(x => new { x.storeid, x.storeName, x.storeDress, x.discount, property = x.property == 1 ? "大号" : x.property == 3 ? "小号" : x.property == 2 ? "结算台+冰箱" : x.property == 4 ? "结算台+货架" : "货架+冰箱" }).ToList();
                foreach (var item in list)
                {
                    string storeid = item.storeid.ToString();
                    //查询该店铺 对应的理货员 id   
                    int managerCode = managerStroreBll.Search(x => x.storeid == storeid).Select(x => new { x.managerCode }).FirstOrDefault().managerCode;
                    //根据 managerid  查询 manager表的信息
                    string name = managerBll.Search(x => x.userid == managerCode && x.state == 1).Select(x => new { x.RealName, }).FirstOrDefault().RealName;
                    //添加到 list 集合
                    StoreModel storemodel = new StoreModel();
                    storemodel.storeid = item.storeid;
                    storemodel.storeName = item.storeName;
                    storemodel.storeDress = item.storeDress;
                    storemodel.RealName = name;
                    storemodel.property = item.property;
                    storemodel.discount = item.discount.ToString();
                    storeList.Add(storemodel);
                }
            }
            return Json(new { rows = storeList, total = count });
        }



        public ActionResult GetManager(int storeid)
        {
            var managerList = new List<StoreModel>();
            string stoid = storeid.ToString();
            var codelist = managerStroreBll.Search(x => x.storeid == stoid).Select(x => new { x.managerCode }).ToList();
            foreach (var item in codelist)
            {
                //根据 managerid  查询 manager表的信息
                var manager = managerBll.Search(x => x.userid == item.managerCode).Select(x => new { x.userid, x.RealName, x.userPhone, x.state }).ToList();
                //添加到 list 集合 userid state
                StoreModel storemodel = new StoreModel();
                storemodel.RealName = manager.FirstOrDefault().RealName;
                storemodel.userPhone = manager.FirstOrDefault().userPhone;
                storemodel.userid = manager.FirstOrDefault().userid;
                storemodel.state = manager.FirstOrDefault().state == 1 ? "在职" : "离职";
                managerList.Add(storemodel);
            }
            return Json(managerList);

        }


        /// <summary>
        /// 添加店铺 并添加货架对应的商品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save2Wares(store model, string managerCode)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            managerstore mastoeModel = new managerstore();
            if (model.storeName != "" && model.storeDress != "")
            {
                model.areaCode = "11001";
                model.registertime = DateTime.Now;
                model.storeStatus = 1;
                //  model.discount
                //往店铺表里添加信息
                if (bll.Add(model) > 0)
                {
                    // 在 往 managerStore表里添加信息
                    //int mCode = Convert.ToInt32(managerCode);
                    //mastoeModel.managerCode = Convert.ToInt32(managerCode);
                    //mastoeModel.storeid = model.storeid.ToString();
                    //mastoeModel.areaCode = 11001;

                    //if (managerStroreBll.Add(mastoeModel) > 0)
                    //{
                        //***********添加店铺成功之后 往waresDetails 表里 添加数据
                        //
                        //根据新增的店铺型号  查找相应的模板中商品 
                        var tempList = tempBll.Search(x => x.tempAttribute == model.property).ToList();
                        foreach (var item in tempList)
                        {
                            waresdetails waresDetailsModel = new waresdetails();
                            waresDetailsModel.storeid = model.storeid;
                            waresDetailsModel.validstatus = 1;
                            waresDetailsModel.waresBatch = DateTime.Now;
                            waresDetailsModel.waresCode = item.waresCode;
                            waresDetailsModel.waresSupportNum = item.waresSupportNum;//货架承受数量
                            waresDetailsModel.waresquantity = 0;//实际数量
                            waresDetailsModel.waresMinNum = item.waresMinNum;//商品预警值
                            waresDetailsModel.sign = 1;
                            waresDetailBll.Add(waresDetailsModel);
                        }
                        msg = "添加成功";
                        result = 1;
                        jieguo = 1;
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                    else
                    {
                        jieguo = 1;
                        msg = "添加失败";
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                }
                 return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
            //else
            //{
            //    jieguo = 0;
            //    msg = "请填写完整";
            //    return Json(new { jieguo = jieguo, state = result, msg = msg });
            //}
           
  


        ////获取选中的一条数据
        //public ActionResult GetOnlyOne1(int id)
        //{
        //    var storeGetmodelList = new List<StorGeteModel>();
        //    StorGeteModel storeGetmodel = new StorGeteModel();
        //    string sid = id.ToString();
        //    //根据店铺ID 查询对应的理货员ＩＤ
        //    int managerCode = managerStroreBll.Search(x => x.storeid == sid).Select(x => new { x.managerCode }).FirstOrDefault().managerCode;
        //    var list = bll.Search(x => x.storeid == id).ToList().Select(x => new { x.storeName, x.storeid, x.storeDress, x.property, x.discount }).ToList()[0];
        //    storeGetmodel.discount = list.discount;
        //    storeGetmodel.property = list.property;
        //    storeGetmodel.storeDress = list.storeDress;
        //    storeGetmodel.storeName = list.storeName;
        //    storeGetmodel.userid = managerCode;
        //    storeGetmodel.storeid = list.storeid;
        //    storeGetmodelList.Add(storeGetmodel);
        //    return Json(storeGetmodelList[0], JsonRequestBehavior.AllowGet);
        //}

        //获取选中的一条数据 修改版
        public ActionResult GetOnlyOne(int id)
        {
            return Json(bll.Search(x => x.storeid == id).ToList().Select(x => new {
                x.storeid,
                x.storeName,
                x.storeDress,
                x.property,
                x.discount,
                x.storeStatus
            }).ToList()[0], JsonRequestBehavior.AllowGet);
        }

        //获取理货员列表
        public ActionResult GetManagerName()
        {

            return Json(managerBll.Search().Where(x => x.state == 1).Select(x => new { UserID = x.userid, UserName = x.RealName }).ToList());
        }

        //修改 相关信息
        public ActionResult Modify1(store model, int managerCode)
        {
            managerstore mastoeModel = new managerstore();
            string msg = "修改失败";
            string state = "false";
            model.registertime = bll.Search(x => x.storeid == model.storeid).Select(x => new { x.registertime }).FirstOrDefault().registertime;
            model.storeStatus = 1;
            model.areaCode = "11001";
            if (model.storeName != "" && model.storeDress != "")
            {
                //修改 store 相关信息    
                if (bll.Modify(model) > 0)
                {
                    //修改 managerStore 相关信息  
                    // 店铺ID不会变化  根据店铺ID 查找对应的 managerCode 和 关联表 ID
                    mastoeModel.managerCode = managerCode;
                    mastoeModel.storeid = model.storeid.ToString();
                    var list = managerStroreBll.Search(x => x.storeid == mastoeModel.storeid);
                    if (list.Count() != 0)
                    {
                        int id = list.Select(x => new { x.id }).FirstOrDefault().id;
                        mastoeModel.id = id;
                        mastoeModel.areaCode = 11001;
                        if (managerStroreBll.Modify(mastoeModel) > 0)
                        {
                            msg = "修改成功";
                            state = "true";
                        }
                        else
                        {
                            msg = "修改失败";
                            state = "false";
                        }
                    }
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }
            return Json(new { state = state, msg = msg });
        }

        public  ActionResult Modify2(store model)
        {
            string msg = "修改失败";
            string state = "false";
            model.registertime = bll.Search(x => x.storeid == model.storeid).Select(x => new { x.registertime }).FirstOrDefault().registertime;
            model.areaCode = "11001";
            model.storeStatus = 1;
            if (model.storeName != "" && model.storeDress != "")
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }

            return Json(new { state = state, msg = msg });
        }

        //删除  店铺  软删除 
        public ActionResult ModifyState(int id)
        {
            store model = new store();
            string msg = "删除店铺失败";
            string state = "false";
            var list = bll.Search(x => x.storeid == id).FirstOrDefault();
            model.storeid = id;
            model.storeStatus = 0;
            model.areaCode = "11001";
            model.discount = list.discount;
            model.property = list.property;
            model.registertime = list.registertime;
            model.storeName = list.storeName;
            model.storeDress = list.storeDress;
            if (model.storeName != "" && model.storeDress != "")
            {
                //修改 store 相关信息     
                if (bll.Modify(model) > 0)
                {
                    msg = "删除店铺成功";
                    state = "true";
                }
                else
                {
                    msg = "删除店铺失败";
                    state = "false";
                }
            }
            return Json(new { state = state, msg = msg });
        }




    }
}
